package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.Category;
import com.dropdb.demo.bean.CategoryExample;
import com.dropdb.demo.mapper.CategoryMapper;
import com.dropdb.demo.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 点击分类获取大类别和第一个类别的详细信息
     * @return
     */
    @Override
    public HashMap<String, Object> getIndexOfCatalog() {
        // 获取所有大类别
        CategoryExample example = new CategoryExample();
        example.createCriteria().andPidEqualTo(0);
        List<Category> categoryList = categoryMapper.selectByExample(example);
        // 防止空指针异常
        if (categoryList.isEmpty()) return null;
        // 获取当前大类别
        Category currentCategory = categoryList.get(0);
        HashMap<String, Object> map = new HashMap<>();
        map.put("currentCategory", currentCategory);
        map.put("categoryList", categoryList);
        // 获取第一个大类别中的小类别
        CategoryExample subCateExample = new CategoryExample();
        subCateExample.createCriteria().andPidEqualTo(currentCategory.getId());
        List<Category> subCategories = categoryMapper.selectByExample(subCateExample);
        map.put("currentSubCategory", subCategories);
        return map;
    }


    /**
     * 获取当前类别下详细信息
     * @param id
     * @return
     */
    @Override
    public HashMap<String, Object> getCurrentCatalog(Integer id) {
        // 获取当前大类别
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category == null) return null;
        // 根据大类别id获取小类别
        CategoryExample example = new CategoryExample();
        example.createCriteria().andPidEqualTo(category.getId());
        List<Category> currentSubCategory = categoryMapper.selectByExample(example);
        HashMap<String, Object> map = new HashMap<>();
        map.put("currentCategory", category);
        map.put("currentSubCategory", currentSubCategory);
        return map;
    }
}
